﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Track Collections Parser</title>

        <script src="/thirdparty/js/jquery-1.4.2.min.js"    type="text/javascript"></script>
        <script src="/thirdparty/js/jquery.json-2.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            var g_track_list = [];
            var continue_upload = false;
            function UploadOneTrack( data )
            {
                if ( !continue_upload )
                {
                    $("#source").append( "<p><b>Upload aborted!</b></p>" );
                    return;
                }
                
                if (data) $("#source").append( "<p>" + data + "</p>" );
                if ( g_track_list.length == 0 ) 
                {
                    $("#source").append( "<p><b>Done!</b></p>" );
                    continue_upload = false;
                    return;
                }
    
                //var curData = g_track_list.shift();
                var curData = {};
                var trackData = g_track_list.shift();
                curData.track_data = $.toJSON(trackData);
                curData.tag_names = trackData.tag_names;
                
                $("#source").append( "<p><b> Uploading file ("  + (g_track_list.length+1) + " left): " 
                                     + trackData.track_url + "</b></p>" );
                
                //curData.tag_ids = "";
                curData.skip_duplicates = 1;
                
                $.ajax({ url: "cgi-bin/track_upload.cgi", 
                         type : "POST",
                         dataType: "html", 
                         success: UploadOneTrack,
                         data: curData
                      });
            }
            function initialize()
            {
                var collectionName = document.getElementById("collection_select").value;
                //collections: turpohod, slazav_old, slazav
                $.ajax({ url: "cgi-bin/parse_collection.cgi?collection=" + collectionName, 
                         dataType : "json",                          
                         success: function( parse_results )
                {
                    //$("#source").append($("<pre></pre>").text(parse_results.log));
                    //alert(parse_results.log);
                    //$("#source").empty();
                    //$("#source").append('<pre>' + parse_results.log + '</pre>');
                    //return;
                    
                    g_track_list = parse_results.tracks.slice(0); //clone(?)
                    
                    
                    $("#source").empty();
                    
                    if ( parse_results.log )
                        $("#source").append('<p><i>' + parse_results.log + '</i></p>');
                    
                    continue_upload = true;
                    
                    UploadOneTrack();
                }});
            }
        </script>
        
    </head>

    <body>
        <form>
            <select id = "collection_select" name="collection">
                <option value="turpohod" selected>Группа Рыжавского</option>
                <option value="slazav">Завьялов</option>
                <option value="slazav_old">Завьялов старая</option>
                <option value="dmitriev">Каталог А. Бахвалова</option>
                <option value="google_search">Поиск гугла</option>
            </select>
        </form>
        <form>
            <input type="button" value = "Start upload" onclick = "initialize()"/>
            <input type="button" value = "Stop upload"  onclick = "continue_upload = false"/>
        </form>
        <div id = "source"></div>
    </body>
</html>